<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/22
 * Time: 10:08
 */

namespace app\api\controller;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,fs-token");
header('Access-Control-Allow-Methods: GET, POST');
use app\api\model\Exam as ExamModel;
use app\api\validate\IDMustBePositiveInt;
use app\api\model\User as UserModel;
use think\Db;

/**
 * @title 考试接口
 * @group app
 */
class Exam extends  Base
{
    /**
     * @title 根据地区名称获取考试相关信息
     * @author 开发者
     * @url /api/Exam/getInfoByRegion
     * @method POST/GET
     * @description 测试地址:北京
     * @param name:region require:1 type:varchar default:null desc：选择的地区名称
     * @return id:考试的主键id
     * @return name:考试的名称
     * @return intro:考试说明
     * @return exam_time:考试时间
     * @return region:地区
     * @return school:学校
     * @return create_time:创建时间
     * @return total_user: 总人数
     * @return subjects:科目@
     * @subjects name:科目名称
     */
    public function getInfoByRegion(){
        //获取地区名称
        $region = input('region');
        if(!$region)$this->apiReturn(303,'请选择相关地区！');
        $result = ExamModel::getInfoByRegion($region);
        if(empty($result)){
            $this->apiReturn(303,'未找到相关信息！');
        }else{
            $this->apiReturn(200,'成功获取信息！',$result);
        }
    }
    /**
     * @title 获取模拟考试的学生分数，排名信息
     * @author 开发者
     * @url /api/Exam/getGradeById
     * @method POST/GET
     * @description 测试联考id为1
     * @param name:id require:1 default:null type:int desc:联考id
     * @return id:模拟考试id
     * @return name:考试名称
     * @return region:考试地区
     * @return school:学校
     * @return total_grade:用户列表@
     * @total_grade id:分数表的主键id score:总分数 user:学员信息@
     * @user id:学生id name:学生姓名 img:头像 school:学校 gender:性别 1 男 2女
     */
    public  function getGradeById(){
        //获取传递的参数id
        $id = input('id');
        //对id进行验证
        (new IDMustBePositiveInt())->goCheck();
        $result = ExamModel::getGradeById($id);
       if($result->isEmpty()){
           $this->apiReturn(303,'未找到相关信息！');
       }else{
           $this->apiReturn(200,'获取信息成功！',$result);
       }
    }
    /**
     * @title 获取考试容及科目的相关信息
     * @author 开发者
     * @url /api/Exam/getExamInfoById
     * @method POST/GET
     * @description 测试联考id为1
     * @param name:id require:1 default:null type:int desc:联考id
     * @return id:考试id
     * @return name:模拟考试的名称
     * @return intro:模拟考试说明
     * @return exam_time:考试时间
     * @return  region:地区
     * @return create_time:创建时间
     * @return subjects:科目@
     * @subjects id:科目id  name:科目名称 price:价格 content:内容 img:图片地址 exam_time:考试时间
     */
     public function  getExamInfoById(){
         //获取传递的参数id
         $id = input('id');
         //对id进行验证
         (new IDMustBePositiveInt())->goCheck();
         $result = ExamModel::getExamInfoById($id);
         if($result->isEmpty()){
             $this->apiReturn(303,'未找到相关信息！');
         }else{
             $this->apiReturn(200,'获取信息成功！',$result);
         }
     }
    /**
     * @title 获取用户的所有联考信息及排名
     * @author 开发者
     * @url /api/exam/getExamInfoByUser
     * @method POST/GET
     * @param name:id require:1 default:null type:int desc:用户id
     * @return id:用户id
     * @return username:用户名
     * @return img:用户头像
     * @return phone:手机号
     * @return user_exam:用户联考信息@
     * @user_exam id:联考id name:联考名称 intro:联考说明 exam_time:考试时间 region:地区 school:学校 create_time:创建时间 score:总分数 rank:排名
     */
    public function getExamInfoByUser(){
        $user_id = input('id');
        (new IDMustBePositiveInt())->goCheck();
        $result =UserModel::getPersonExamInfoById($user_id);
        foreach ($result['user_exam'] as $k=>$v){
            //删除关联查询产生的中间表数据
            unset($v['pivot']);
            //获取考试id
            $exam_id =$v['id'];
            //根据用户id 和考试id 进行总分排名
            $data = Db::query("select score,exam_id,(select count(distinct score) from art_total_grade
              as tg2 where tg2.score>=tg1.score and tg2.exam_id = tg1.exam_id) rank from art_total_grade as tg1 where tg1.user_id =$user_id
             and tg1.exam_id=$exam_id order by score desc");
           foreach ($data as $key =>$val){
               //将获取到的排名和总分数 添加到要返回信息数据中
               $v['score'] = $val['score'];
               $v['rank '] = $val['rank'];
           }
        }
        $this->apiReturn(200,'',$result);
    }


}